﻿import mx.utils.Delegate;
class cn.eminia.RClassV3.Page {
	// [置顶]
	// 
	// var pg = new cn.eminia.RClassV3.Page('-', {msk:msk, page:page});
	// var pg = new cn.eminia.RClassV3.Page('|', {msk:msk, page:page});
	var _page, _msk, _orientation, ctrl, pos, _rollck;
	var per;
	var tw = true;
	static var HORIZONTAL:String = "-";
	static var VERTICAL:String = "|";
	var _max:Number;
	var _min:Number;
	var _mskScope;
	var mouseListener:Object;
	function Page(orientation:String, obj:Object) {
		_orientation = orientation;
		applyMC(obj);
	}
	function updatePos(prop, oldVal, newVal) {
		//trace(newVal);
		if (_orientation == HORIZONTAL) {
			this.pos = -newVal*(_page._width-_msk._width)+_msk._x;
		} else {
			this.pos = -newVal*(_page._height-_msk._height)+_msk._y;
		}
	}
	function applyMC(obj) {
		_mskScope = obj.mskScope;
		_page = obj.page;
		_msk = obj.msk;
		_rollck = obj.rollck;
		//
		if (_orientation == HORIZONTAL) {
			_min = -(_page._width-_msk._width)+_msk._x;
			_max = _msk._x;
		} else {
			_min = -(_page._height-_msk._height)+_msk._y;
			_max = _msk._y;
		}
		//
		_page.setMask(_msk);
		ctrlPPP();
		addMMM();
	}
	function dispose() { 
		trace("Page::dispose = " );
		deleteMMM();
		delete ctrl.onEnterFrame;
		removeMovieClip(ctrl);
	}
	function ctrlPPP() {
		var dd = _root.getNextHighestDepth();
		ctrl = _root.createEmptyMovieClip('RClassV3_Page'+dd, dd);
		ctrl.onEnterFrame = Delegate.create(this, function () {
			//trace('RClassV3_Page'+ctrl._name);
			if (_orientation == HORIZONTAL) {
				_page._x = (tw) ? _page._x+(pos-_page._x)*.3 : pos;
				_root.tf.text;
			} else {
				_page._y = (tw) ? _page._y+(pos-_page._y)*.3 : pos;
			}
		});
	}
	
	private function deleteMMM():Boolean  { 
		trace("mouseListener = " + mouseListener); 
		var b:Boolean = Mouse.removeListener(this.mouseListener);
		
		trace("b = " + b);  
		return b;
	}
	private function addMMM():Void {
		mouseListener = new Object();
		mouseListener.onMouseWheel = Delegate.create(this, function (delta) {
			if (!needWheel()) {
				return;
			}
			//                 
			//
			if (_orientation == HORIZONTAL) {
				this.pos = correctValue(_page._x-(-delta*15));
				per = -(this.pos-_msk._x)/(_page._width-_msk._width);
			} else {
				this.pos = correctValue(_page._y-(-delta*15));
				per = -(this.pos-_msk._y)/(_page._height-_msk._height);
			}
		});
		Mouse.addListener(mouseListener);
	}
	private function correctValue(n:Number):Number {
		n = Math.min(n, _max);
		n = Math.max(n, _min);
		return n;
	}
	private function needWheel() {
		if (_rollck && !_rollck()) {
			return false;
		}
		if (((_mskScope == null) ? _msk : _mskScope).hitTest(_xmouse, _ymouse)) {
			return true;
		} else {
			return false;
		}
	}
}
